﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FCTC_Web.Models;
using FCTC_Web.DAL;
using DAL;
using FCTC_Web.Utilities;

namespace FCTC_Web.Controllers.Maintains
{ 
    public class VehicleController : Controller
    {

        [HandleError]
        public ViewResult Index(int? pageId, string CarBrand, string BrandType, string VehicleStyle)
        {
            SetBrandTypeStyle(CarBrand, BrandType, VehicleStyle);

            ViewBag.Search = "?CarBrand=" + CarBrand + "&&BrandType=" + BrandType + "&&VehicleStyle=" + VehicleStyle;

            return View("Index", Pager20.Ins.GetPage<Vehicle>(pageId, i => i.CarBrand.Contains(CarBrand == null ? string.Empty : CarBrand)
                    && i.BrandType.Contains(BrandType == null ? string.Empty : BrandType)
                    && i.VehicleStyle.Contains(VehicleStyle == null ? string.Empty : VehicleStyle)));

        }

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            if (ModelState.IsValid)
            {
                CH.Delete<Vehicle>(id);
                var pageId = Pager20.Ins.GetPageId<Vehicle>(id);
                return RedirectToAction("Index", new { pageId = pageId });
            }
            Vehicle vehicle = CH.GetDataById<Vehicle>(id);
            return View(vehicle);
        }

        [HttpPost]
        public ActionResult Create(Vehicle vehicle)
        {

            if (ModelState.IsValid)
            {
                CH.Create<Vehicle>(vehicle);
                var pageId = Pager20.Ins.GetPageId<Vehicle>(vehicle.ID);
                return RedirectToAction("Index", new { pageId = pageId });
            }
            InitialDDL(vehicle);
            return View(vehicle);
        }

        [HttpPost]
        public ActionResult Edit(Vehicle vehicle)
        {
            if (ModelState.IsValid)
            {
                CH.Edit<Vehicle>(vehicle);
                var pageId = Pager20.Ins.GetPageId<Vehicle>(vehicle.ID);
                return RedirectToAction("Index", new { pageId = pageId });
            }
            InitialDDL(vehicle);
            return View(vehicle);
        }

        public void SetBrandTypeStyle(string brand, string selection, string style)
        {
            var ns = VehicleXmlHelper.Brands;
            ViewBag.CarBrand = new SelectList(ns);
            if (!string.IsNullOrWhiteSpace(brand))
                ViewBag.BrandType = new SelectList(VehicleXmlHelper.GetSelectiosn(brand));
            else
                ViewBag.BrandType = new SelectList(new List<string>());

            if (!string.IsNullOrWhiteSpace(selection))
                ViewBag.VehicleStyle = new SelectList(VehicleXmlHelper.GetvehicleType(brand, selection));
            else
                ViewBag.VehicleStyle = new SelectList(new List<string>());
        }

        public ViewResult Details(int? id)
        {
            Vehicle vehicle = CH.GetDataById<Vehicle>(id);
            return View(vehicle);
        }

        public ActionResult Create()
        {
            InitialDDL();
            return View();
        }

        public PartialViewResult BrandUpdateType(string brand)
        {
             if (!string.IsNullOrWhiteSpace(brand))
                 ViewBag.BrandType = new SelectList(VehicleXmlHelper.GetSelectiosn(brand));
            else
                 ViewBag.BrandType = new SelectList(new List<string>());
             return PartialView(@"~\views\vehicle\partial\brandtype.cshtml");
        }

        public PartialViewResult BrandUpdateStyle(string style)
        {
            ViewBag.VehicleStyle = new SelectList(new List<string>());
            return PartialView(@"~\views\vehicle\partial\vehicletype.cshtml");
        }

        public PartialViewResult TypeUpdateStyle(string brand, string type)
        {
            if (!string.IsNullOrWhiteSpace(brand) && !string.IsNullOrWhiteSpace(type))
                ViewBag.VehicleStyle = new SelectList(VehicleXmlHelper.GetvehicleType(brand, type));
            else
                ViewBag.VehicleStyle = new SelectList(new List<string>());

            return PartialView(@"~\views\vehicle\partial\vehicletype.cshtml");

        }

        public ActionResult Edit(int? id)
        {
            var vehicle = CH.GetDataById<Vehicle>(id);
            InitialDDL(vehicle);
            return View(vehicle);
        }

        private void InitialDDL(Vehicle vehicle=null)
        {
            var brands = VehicleXmlHelper.Brands;

            if (vehicle == null)
            {
                ViewBag.ShopID = DrapDownListHelper.InitialSelectionDropDownList<Shop>("ID", "ID", "FullName");
                ViewBag.EstimatorID = DrapDownListHelper.InitialSelectionDropDownList<Estimator>("ID", "ID", "Personal.FullName");
                ViewBag.CarBrand = new SelectList(brands,null);
                ViewBag.BrandType = new SelectList(new List<string>());
                ViewBag.VehicleStyle = new SelectList(new List<string>());
                return;
            }
          
            ViewBag.CarBrand = new SelectList(brands, vehicle.CarBrand);
            var selections = VehicleXmlHelper.GetSelectiosn(vehicle.CarBrand);
            ViewBag.BrandType = new SelectList(selections, vehicle.BrandType);
            var types = VehicleXmlHelper.GetvehicleType(vehicle.CarBrand, vehicle.BrandType);
            ViewBag.VehicleStyle = new SelectList(types, vehicle.VehicleStyle);
            ViewBag.ShopID = DrapDownListHelper.InitialSelectionDropDownList<Shop>("ID", "ID", "FullName", vehicle.ShopID);
            ViewBag.EstimatorID = DrapDownListHelper.InitialSelectionDropDownList<Estimator>("ID", "ID", "Personal.FullName", vehicle.EstimatorID);
        }

        public ActionResult Delete(int? id)
        {
            Vehicle vehicle = CH.GetDataById<Vehicle>(id);
            return View(vehicle);
        }

        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);
        }
    }
}